import { defineStore } from "pinia";
import { essay_add, essay_list, update_one_essay, delete_one } from "@/apis/EssayApi"
import { type Eassay, type Eassay_detaile } from "@/types/Eassy"
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const EssayStore = defineStore('EssayStore', () => {
    const router = useRouter()
    const add_essay = async (data: Eassay) => {
        const result: any = await essay_add(data)
        if (result.code = 200) {
            router.back()
            ElMessage({
                type: "success",
                message: "添加成功"
            })

        } else {
            ElMessage({
                type: "error",
                message: "添加失败"
            })
        }
    }
    const get_list = async (currentPage: number) => {
        const result: any = await essay_list(currentPage)
        return {
            data: result.data,
            count: result.count
        }
    }
    const update_one = async (data: Eassay_detaile) => {
        const result: any = await update_one_essay(data)
        if (result.code = 200) {
            router.back()
            ElMessage({
                type: "success",
                message: "修改成功"
            })

        } else {
            ElMessage({
                type: "error",
                message: "修改失败"
            })
        }

    }
    const delete_essay = async (_id: string) => {
        const result: any = await delete_one(_id)
        if (result.code == 200) {
            ElMessage({
                type: 'success',
                message: "删除成功"
            })
        } else {
            ElMessage({
                type: 'error',
                message: "删除失败"
            })
        }
    }
    return {
        add_essay,
        get_list,
        update_one,
        delete_essay
    }
})


export default EssayStore